package com.xlf.controller.admin;

import com.xlf.domain.Role;
import com.xlf.service.MenuService;
import com.xlf.service.RoleService;
import com.xlf.utils.ResponseResult;
import com.xlf.vo.IsDisableVO;
import com.xlf.vo.QueryPageBean;
import com.xlf.vo.RoleVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 后台角色管理控制层
 */
@Tag(name = "后台角色管理控制层")
@RestController
@RequestMapping("/role")
@PreAuthorize("hasAuthority('role')")
public class RoleController {

    @Resource
    private RoleService roleService;

    @Resource
    private MenuService menuService;


    /**
     * 查询所有角色
     * @return  执行结果
     */
    @Operation(summary = "查询所有角色")
    @GetMapping("/listAllRoles")
    public ResponseResult listAllRoles() {
        return ResponseResult.success("获取后台角色数据", roleService.list());
    }


    /**
     * 条件查询角色列表，支持分页、时间区间、分类/标签id查询
     * @param queryPageBean 分页查询条件对象
     * @return              执行结果
     */
    @Operation(summary = "条件查询角色列表", description = "支持分页、时间区间、分类/标签id查询")
    @PostMapping("/listRoles")
    @PreAuthorize("hasAuthority('role:listRoles')")
    public ResponseResult listRole(@RequestBody QueryPageBean queryPageBean) {
        return ResponseResult.success("获取后台角色数据", roleService.listRoles(queryPageBean));
    }


    /**
     * 删除角色
     * @param roleIdList 角色id列表
     * @return {@link ResponseResult}
     */
    @Operation(summary = "删除角色")
    @DeleteMapping("/delete")
    @PreAuthorize("hasAuthority('role:delete')")
    public ResponseResult deleteRoles(@RequestBody List<Long> roleIdList) {
        roleService.removeByIds(roleIdList);
        return ResponseResult.success("删除角色成功");
    }

    /**
     * 保存或更新角色
     * @param roleVO 角色信息
     * @return {@link ResponseResult}   执行结果
     */
    @Operation(summary = "保存或更新角色")
    @PostMapping("/saveOrUpdateRole")
    @PreAuthorize("hasAuthority('role:saveOrUpdateRole')")
    public ResponseResult saveOrUpdateRole(@RequestBody RoleVO roleVO) {
        roleService.saveOrUpdateRole(roleVO);
        return ResponseResult.success("编辑角色信息成功");
    }

    /**
     * 启用/禁用角色
     * @param isDisableVO   菜单项显示开关对象
     * @return              执行结果
     */
    @Operation(summary = "启用/禁用角色")
    @PostMapping("/isDisable")
    @PreAuthorize("hasAuthority('role:isDisable')")
    public ResponseResult isDisable(@RequestBody IsDisableVO isDisableVO){
        final Role byId = roleService.getById(isDisableVO.getId());
        byId.setStatus(isDisableVO.getIsDisable());
        roleService.updateById(byId);
        return ResponseResult.success("修改成功");
    }

    /**
     * 获取权限列表
     * @return  执行结果
     */
    @Operation(summary = "获取权限列表")
    @GetMapping("/listMenus")
    public ResponseResult listMenus(){
        return ResponseResult.success(menuService.listMenusLabel());
    }
}
